<?php

namespace app\shop\controller;

// 框架基础控制类
use think\Controller;

class Business extends Controller
{
    //构造函数
    public function __construct()
    {
        //手动把父类的方法获取过来
        parent::__construct();

        // check_cors_request();

        // 全局用户模型
        $this->BusinessModel = model('Business.Business');
    }

    // 注册
    public function register()
    {

        if ($this->request->isPost()) {

            $mobile = $this->request->param('mobile', '', 'trim');

            $password = $this->request->param('password', '', 'trim');

            // 判断当前手机号码是否已注册
            $userinfo = $this->BusinessModel->where('mobile', $mobile)->find();

            if ($userinfo) {
                $this->error('当前手机号码已注册，请重新输入');
            }

            // 密码盐
            $salt = randstr();

            // 密码加密
            $password = md5($password . $salt);

            $data = [
                'nickname' => $mobile,
                'mobile' => $mobile,
                'password' => $password,
                'salt' => $salt
            ];

            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

            if ($result) {
                $this->success('注册成功');
            } else {
                $this->error($this->BusinessModel->getError());
            }
        }
    }

    // 登陆
    public function login()
    {
        if ($this->request->isPost()) {

            $mobile = $this->request->param('mobile', '', 'trim');

            $password = $this->request->param('password', '', 'trim');

            // 查找当前手机号码
            $userinfo = $this->BusinessModel->where('mobile', $mobile)->find();

            if (!$userinfo) {
                $this->error('当前用户不存在');
            }

            $password = md5($password . $userinfo['salt']);

            if ($password != $userinfo['password']) {
                $this->error('密码错误，请重新输入');
            }

            $data = [
                'id' => $userinfo['id'],
                'mobile' => $userinfo['mobile'],
                'nickname' => $userinfo['nickname'],
                'avatar' => $userinfo['avatar_text'],
                'gender' => $userinfo['gender'],
                'province' => $userinfo['province'],
                'city' => $userinfo['city'],
                'district' => $userinfo['district'],
                'region' => $userinfo['region'],
                'createtime' => $userinfo['createtime'],
                'money' => $userinfo['money'],
            ];

            $this->success('登陆成功', '',  $data);
        }
    }

    // 修改资料
    public function profile()
    {
        // 关闭模板布局
        $this->view->engine->layout(false);

        $id = isset($this->LoginAuth['id']) ? $this->LoginAuth['id'] : 0;
        // 省份
        $province = $this->RegionModel->where('code', $this->LoginAuth['province'])->value('name');

        // getLastSql()调式sql语句 打印原生sql语句
        // echo  $this->RegionModel->getLastSql();exit;
        // 市
        $city = $this->RegionModel->where('code', $this->LoginAuth['city'])->value('name');
        // 区
        $district = $this->RegionModel->where('code', $this->LoginAuth['district'])->value('name');

        if ($this->request->isPost()) {
            $params = $this->request->param('');
            $data = [
                'id' => $id,
                'nickname' => $params['nickname'],
                'gender' => $params['gender'],
            ];

            // 是否修改登陆密码
            if ($params['password']) {
                // 修改的新密码加密
                $password = md5($params['password'] . $this->LoginAuth['salt']);
                if ($password == $this->LoginAuth['password']) {
                    $this->error('修改的密码与原密码一致,请重新输入');
                }

                $data['password'] = $password;
            }

            // 修改省市区
            if ($params['region']) {
                $region = explode('/', $params['region']);
                if (count($region) < 3) {
                    $this->error('请重新选择省市区!');
                }
                $pathcode = $this->RegionModel->where('name', $region[2])->value('parentpath');
                $pathcode = explode(',', $pathcode);
                list($province, $city, $district) = $pathcode;
                $data['province'] = $province;
                $data['city'] = $city;
                $data['district'] = $district;
            }

            // 是否文件上传
            if ($_FILES["avatar"]["error"] == 0 && $_FILES["avatar"]["size"]) {
                $res = upload('avatar');
                if ($res['code'] == 1) {
                    $data['avatar'] = $res["url"];
                } else {
                    $this->error('图片上传失败');
                }
            }


            $result = $this->BusinessModel->isUpdate()->save($data);
            if ($result) {

                $this->success('修改成功');
            } else {
                $this->error($this->BusinessModel->getError());
            }
        }

        // 模板变量赋值
        $this->assign([
            'province' => $province,
            'city' => $city,
            'district' => $district
        ]);

        // 模块渲染
        return $this->view->fetch();
    }
}
